import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.ArrayList;

/**
 * Description:
 * 
 *      User selects a date and the program retrieves the 
 *      daily temperature within a one year range.
 * 
 * Author: Chase Conner
 * Date:   November 3, 2016
 * Course: CSCI 2002-91: Structure of Computer Programming II
 */
public class FileIO
{
    private String path;
    private String delimiter;
    private SortedList<Double> temperatures = new SortedList();
    private List<String> months = new ArrayList(); // Index corresponds with temperatures SortedList since it is added at the same time as temperatures
    
    public FileIO(String path, String delimiter)
    {
        this.path = path;
        this.delimiter = delimiter;
    }
    
    public void read(String month, String day, String year, int numberOfYears) throws FileNotFoundException
    {
        Scanner scanner = new Scanner(new File(path)).useDelimiter(delimiter);
        boolean start = false; // Start adding all the temperatures hitherto once the desired date is found 
        String scannedMonth = null;
        String scannedDay = null; 
        String scannedYear = null;
        double scannedTemp;
        String date = date(month, day, year);
        //System.out.println("Date: " + date);
        //System.out.println("Scanned Date: " + scanner.nextLine());
        while (scanner.hasNextLine() && !scanner.nextLine().equals(date))
        {
            String[] line = scanner.nextLine().split(delimiter); // Splits the line to evaluate the month, day, and year
            
            scannedMonth = line[0]; // Index 0 is the scanned month after being read from file
            scannedDay = line[1];   // Index 1 is the scanned day after being read from file
            scannedYear = line[2];  // Index 2 is the scanned year after being read from file
            scannedTemp = Double.parseDouble(line[3]);  // Index 3 is the scanned temperature
            
            // If the passed arguments for month, day, and the year match
            // up with the file's, add temperature to SortedList
            if (scannedMonth.equals(month) && scannedDay.equals(day) && scannedYear.equals(year))
            {
                temperatures.add(scannedTemp);
                months.add(scannedMonth);
                start = true; // Flag to start adding temperatures to SortedList
            }
            
            // Once the desired date is found, begin adding 
            // the temperature for every day
            if (start == true)
            {
                temperatures.add(scannedTemp);
                months.add(scannedMonth);
            }
        }
        //while (scanner.hasNextLine() && !scanner.nextLine().equals(date)); // Break the loop once you've added all the temperatures within a 1 year range. Ex. 1/5/1995 to 1/5/1996
        
        scanner.close();
        
        //temperatures.print();
        System.out.println("Year Later: " + date(scannedMonth, scannedDay, scannedYear));
        System.out.println("Temperature Size: " + temperatures.size());
        System.out.println("Months Size: " + months.size());
        //System.out.println();
        //print(months);
    }
    
    /**
     * Used to stop Scanner loop by 
     * returning a String of the 
     * current scanned line to see
     * if it matches with the 
     * equals() condition.
     * If yes, terminate loop.
     */
    public String date(String month, String day, String year)
    {
        int addYear = Integer.valueOf(year) + 1;
        String yearLater = Integer.toString(addYear);
        return month + "," + day + "," + (yearLater);
    }
    
    /*
    public SortedList getData()
    {
        List<SortedList> data = new ArrayList();
        SortedList<Integer> jan = new SortedList();
        SortedList<Integer> feb = new SortedList();
        SortedList<Integer> mar = new SortedList();
        SortedList<Integer> april = new SortedList();
        SortedList<Integer> may = new SortedList();
        SortedList<Integer> june = new SortedList();
        SortedList<Integer> july = new SortedList();
        SortedList<Integer> aug = new SortedList();
        SortedList<Integer> sept = new SortedList();
        SortedList<Integer> oct = new SortedList();
        SortedList<Integer> nov = new SortedList();
        SortedList<Integer> dec = new SortedList();
        
        for (int i = 0; i < temperatures.size(); i++)
        {
            int month = months[i]
            switch(month)
            {
                case 1: temperatures.add( 
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10: 
                case 11:
                case 12:
            }
        }
    }
    */
    public void print(List<String> list)
    {
        for (String value : list)
        {
            System.out.print(value + " ");
        }
    }
}
